package com.ym.pms.controller;

import com.github.pagehelper.PageInfo;
import com.ym.pms.model.Item;
import com.ym.pms.service.ItemService;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "项目表接口")
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "查询项目主表中所有项目",notes = "直接查询出项目主表所有内容，包括未启用的项目")
    @GetMapping("/findItemList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int")
    })
    public JSONResult queryItemList(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo<Item> itemList = itemService.findAllItems(pageNum,pageSize);
        return JSONResult.ok(itemList);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "查询项目主表中已启动的所有项目",notes = "直接查询出项目主表所有内容，不包括未启用的项目")
    @GetMapping("/findItemListOfS")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult queryItemListOfS(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo<Item> itemList = itemService.findAllItemsOfS(pageNum,pageSize);
        return JSONResult.ok(itemList);
    }


    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "删除项目",notes = "根据项目编码删除主表所有内容")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "itId", value = "项目的编号", required = true, dataType = "String")
    public JSONResult delete(@RequestParam("itId")String itId){
        int result = itemService.deleteById(itId);
        if (result >= 1) {
            return JSONResult.ok("删除成功");
        } else {
            return JSONResult.errorMsg("删除失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "添加一个项目" , notes = "调用此接口可以在数据表中添加一个对象，其中ItNo为自增属性itId为系统自动生成属性，都不需要填写")
    @PostMapping("/add")
    public JSONResult add(Item record){
        int result = itemService.addItem(record);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else {
            return JSONResult.errorMsg("添加失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "更新项目", notes = "调用此接口，可以更新数据表中已有的数据（itId和ItNo是不允许修改的），" +
            "其中是通过itid查询，故必须要有itid信息；" +
            "要求：前端验证，至少有一条数据有内容（可以和之前的内容相同，不做验证），没有值的字段不放入请求域中")
    @PostMapping(value = "/update")
    public JSONResult update(Item record){
        int result = itemService.update(record);
        if (result >= 1) {
            return JSONResult.ok("更新成功");
        } else if(result == -1){
            return JSONResult.errorMsg("没有获取到itId的值");
        }else{
            return JSONResult.errorMsg("表中未查询到该itId");
        }
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "通过itId查询项目", notes = "调用该接口，传入itId，即可获取该itId所代表的项目的全部信息")
    @GetMapping("/findItemById")
    @ApiImplicitParam(name = "itId", value = "项目的编号", required = true, dataType = "String")
    public JSONResult findById(@RequestParam("itId")String itId){
        Item item = itemService.findById(itId);
        if (item == null){
            return JSONResult.errorMsg("未查询到该itId");
        }else{
            return JSONResult.ok(item);
        }
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "模糊查询，包含所有状态", notes = "调用该接口，以关键字的形式传入itim的参数，" +
            "即可查询到所有符合查询条件的所有信息，返回List<Item>集合;")
    @GetMapping("/fuzzyQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findById(Item item, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        PageInfo<Item> itemPageInfo = itemService.fuzzyQuery(item, pageNum, pageSize);
        if (itemPageInfo.getSize() == 0){
            return JSONResult.errorMsg("未查询到该项目");
        }else{
            return JSONResult.ok(itemPageInfo);
        }
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "模糊查询，仅包含启动项目", notes = "调用该接口，以关键字的形式传入itim的参数，" +
            "即可查询到所有符合查询条件的所有信息，返回List<Item>集合;")
    @GetMapping("/fuzzyQueryOfS")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findByIdOfS(Item item, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        PageInfo<Item> itemPageInfo = itemService.fuzzyQueryOfS(item, pageNum, pageSize);
        if (itemPageInfo.getSize() == 0){
            return JSONResult.errorMsg("未查询到该项目");
        }else{
            return JSONResult.ok(itemPageInfo);
        }
    }

}
